package com.lmf.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmf.eduservice.entity.EduChapter;
import com.lmf.eduservice.entity.EduVideo;
import com.lmf.eduservice.entity.vo.ChapterVo;
import com.lmf.eduservice.entity.vo.VideoVo;
import com.lmf.eduservice.mapper.EduChapterMapper;
import com.lmf.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmf.eduservice.service.EduVideoService;
import com.lmf.handler.Exception.LmfException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lmf
 * @since 2022-01-25
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    
    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id，查询所有的章节实体对象
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(queryWrapper);

        //2.根据课程id，查询所有的小节实体对象
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(queryWrapper2);

        //创建最终返回的章节list集合
        List<ChapterVo> finalChapterList = new ArrayList<>();
        //3.将章节实体对象，封装到响应章节对象里面
        for (EduChapter eduChapter : eduChapters) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

            //4.将小节实体对象，封装到响应小节对象里面
            for (EduVideo eduVideo : eduVideos) {
                //小节对象的章节id与章节对象的id相等，执行响应封装
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    chapterVo.getChildren().add(videoVo);
                }
            }

            //加入到最终返回的集合中去
            finalChapterList.add(chapterVo);
        }
        return finalChapterList;
    }

    //根据章节id，删除章节
    //如果章节下面有小节，就不能直接删除章节，需要先删除完小节，才可以删除章节
    @Override
    public boolean deleteChapterById(String chapterId) throws LmfException {
        //1.根据章节id查询所有的小节
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        List<EduVideo> list = eduVideoService.list(queryWrapper);
        //2.判断是否有小节，再执行章节删除操作，否则抛出异常
        if (list.isEmpty()) {
            //若为空则可以删除章节
            return this.removeById(chapterId);
        }else {
            throw new LmfException(20001,"删除章节失败，章节下面还有小节");
        }
    }

    //2.根据课程id，删除课程里面的章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        this.remove(wrapper);
    }
}
